﻿using System;
using System.Windows.Forms;
using SocialKit.LightRest;

namespace LightRestDemos.Compact
{
    public partial class MainForm : Form
    {
        RestClient client;

        public MainForm()
        {
            InitializeComponent();

            client = new RestClient();
            client.SendAsyncCompleted += client_SendAsyncCompleted;
        }

        void client_SendAsyncCompleted(object sender, SendAsyncCompletedEventArgs e)
        {
            this.Invoke((Action)(() =>
            {
                EnableUI();

                if (e.Error != null)
                {
                    ShowStatus("Error: " + e.Error.Message);
                }
                else
                {
                    ShowStatus(e.Response.Method + " " + e.Response.ResponseUri + " " + (int)e.Response.StatusCode + " " + e.Response.StatusCode);

                    textHeaders.Text = e.Response.Headers.ToString();

                    ShowStatus("Loading response content...(" + e.Response.ContentLength + ")");

                    if (e.Response.ContentType.ToLower().IndexOf("xml") != -1)
                    {
                        var xml = e.Response.ReadContentAsXElement();

                        textBody.Text = xml == null ? "(empty)" : xml.ToString();
                    }
                    else if (e.Response.ContentType.ToLower().IndexOf("text") != -1)
                    {
                        textBody.Text = e.Response.ReadContentAsString();
                    }
                    else
                    {
                        textBody.Text = e.Response.ToString();
                    }

                    ShowStatus("Done.");
                }
            }), null);
        }

        int currentRequest = 0;

        private void menuSend_Click(object sender, EventArgs e)
        {
            Uri uri;
            if (!Uri.TryCreate(comboUri.Text, UriKind.Absolute, out uri))
            {
                MessageBox.Show("Uri is not well formed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                return;
            }

            currentRequest++;
            responseTabs.SelectedIndex = 0;
            tabRequest.Text = "Req #" + currentRequest;

            DisableUI();

            if (!comboUri.Items.Contains(comboUri.Text))
            {
                comboUri.Items.Add(comboUri.Text);
            }

            var request = new HttpRequestMessage(comboMethods.Text, uri);

            if (checkBoxSendBody.Checked)
            {
                ShowStatus("Preparing request content...");

                request.SetContent(EmptyContent.Parse(textContent.Text));
            }

            ShowStatus("Connecting...");

            client.SendAsync(request, currentRequest);
        }

        private void DisableUI()
        {
            this.comboMethods.Enabled = false;
            this.comboUri.Enabled = false;
            this.menuSend.Enabled = false;
            this.textHeaders.Enabled = false;
            this.textBody.Enabled = false;

            //clear texts
            this.textLog.Text = "";
            this.textHeaders.Text = "";
            this.textBody.Text = "";
        }

        private void EnableUI()
        {
            this.comboMethods.Enabled = true;
            this.comboUri.Enabled = true;
            this.menuSend.Enabled = true;
            this.textHeaders.Enabled = true;
            this.textBody.Enabled = true;
        }

        private void ShowStatus(string message)
        {
            this.textLog.Text += Environment.NewLine + message;
        }

        private void menuMenuExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Want to Exit?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        private void menuMenuUploader_Click(object sender, EventArgs e)
        {
            var uf = new UploaderForm();
            uf.ShowDialog();
        }
    }
}